package com.yuanluludev.lib_base.widget

import android.view.View
import androidx.annotation.ColorInt
import com.yuanluludev.lib_base.R
import com.yuanluludev.lib_base.listener.DialogOperationCallback
import com.yuanluludev.lib_utils.util.ColorUtils
import com.yuanluludev.lib_utils.util.ScreenUtils
import com.yuanluludev.lib_utils.util.StringUtils
import com.yuanluludev.lib_base.base.BaseDialogFragment
import kotlinx.android.synthetic.main.common_tip_dialog_layout.*

/**
 * yuanLuLu
 * 简单通用提示弹窗
 */
class CommonTipDialog(
    titleText: String = StringUtils.getString(R.string.common_tip),
    tipText: String = "",
    positiveBtnText: String = StringUtils.getString(R.string.common_confirm),
    negativeBtnText: String = StringUtils.getString(R.string.common_cancel),
    @ColorInt positiveBtnTextColor: Int = ColorUtils.getColor(R.color.color_primary),
    @ColorInt negativeBtnTextColor: Int = ColorUtils.getColor(R.color.color_primary),
    positiveBtnVisible: Boolean = true,
    negativeBtnVisible: Boolean = true,
    operationCallback: DialogOperationCallback? = null
) : BaseDialogFragment() {

    private var titleText: String? = null

    private var tipText: String? = null

    private var positiveBtnText: String? = null

    private var negativeBtnText: String? = null

    private var positiveBtnTextColor: Int? = null

    private var negativeBtnTextColor: Int? = null

    private var positiveBtnVisible: Boolean? = null

    private var negativeBtnVisible: Boolean? = null

    private var operationCallback: DialogOperationCallback? = null

    init {
        this.titleText = titleText
        this.tipText = tipText
        this.positiveBtnText = positiveBtnText
        this.negativeBtnText = negativeBtnText
        this.positiveBtnTextColor = positiveBtnTextColor
        this.negativeBtnTextColor = negativeBtnTextColor
        this.operationCallback = operationCallback
        this.positiveBtnVisible = positiveBtnVisible
        this.negativeBtnVisible = negativeBtnVisible
    }

    override fun initEvents() {

        //设置标题的文本
        titleText?.let {
            tvTitle.text = it
        }

        //设置提示文本
        tipText?.let {
            tvTip.text = it
        }

        //设置积极按钮的文本(默认：确定)
        positiveBtnText?.let {
            tvPositive.text = it
        }

        //设置消极按钮的文本(默认：取消)
        negativeBtnText?.let {
            tvNegative.text = it
        }

        //设置积极按钮文本的颜色
        positiveBtnTextColor?.let {
            tvPositive?.setTextColor(it)
        }

        //设置消极按钮文本的颜色
        negativeBtnTextColor?.let {
            tvNegative?.setTextColor(it)
        }

        //设置消极按钮是否显示
        negativeBtnVisible?.let {
            if (it) tvNegative.visibility = View.VISIBLE else tvNegative.visibility = View.GONE
        }
        //设置积极按钮是否显示
        positiveBtnVisible?.let {
            if (it) tvPositive.visibility = View.VISIBLE else tvPositive.visibility = View.GONE
        }

        //消极按钮的回调
        tvNegative.setOnClickListener {
            operationCallback?.onClickNegativeBtn()
            dismiss()
        }

        //积极按钮的回调
        tvPositive.setOnClickListener {
            operationCallback?.onClickPositiveBtn()
            dismiss()
        }

    }

    override fun getLayoutResId() = R.layout.common_tip_dialog_layout

    override fun getDialogWidth() = (ScreenUtils.getAppScreenWidth() * 0.8).toInt()

}